package com.zjpavt.taiyuan.service.h3cCas;

import com.zjpavt.taiyuan.service.h3cCas.pojo.BasicHostQueryResult;
import com.zjpavt.taiyuan.service.h3cCas.pojo.HostPoolQueryResult;
import com.zjpavt.taiyuan.service.h3cCas.pojo.HostQueryResult;
import com.zjpavt.taiyuan.utils.HttpClientUtil;
import com.zjpavt.taiyuan.utils.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class H3cCasServiceImpl implements H3cCasService{

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<BasicHostQueryResult> basicInfomationQuery(long id) {
        String url = "http://10.20.30.201:8080/cas/casrs/host/summary/"+id;
        List<BasicHostQueryResult> queryResults = null;
        try {
            GetMethod getMethod = new GetMethod(url);
            String sessionId = (String) redisTemplate.boundValueOps("sessionId").get();
            Optional<String> response = Optional.of(HttpClientUtil.get(getMethod, "JSESSIONID=" + sessionId));
            String result = response.get().substring(response.get().indexOf(">")+1);
            //自己实体类的路径
            queryResults = XmlUtil.stringToClass(result, BasicHostQueryResult.class);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return queryResults;
    }

    @Override
    public List<HostPoolQueryResult> hostPoolQuery() {
        String url = "http://10.20.30.201:8080/cas/casrs/hostpool/all";
        List<HostPoolQueryResult> queryResults = null;
        try {
            String sessionId = (String) redisTemplate.boundValueOps("sessionId").get();
            GetMethod getMethod = new GetMethod(url);
            Optional<String> response = Optional.of(HttpClientUtil.get(getMethod, "JSESSIONID=" + sessionId));
            String result = response.get().substring(response.get().indexOf(">")+1);
            //自己实体类的路径
            queryResults = XmlUtil.stringToClass(result, HostPoolQueryResult.class);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return queryResults;
    }

    @Override
    public List<HostQueryResult> hostsQuery() {
        String url = "http://10.20.30.201:8080/cas/casrs/host/";
        List<HostQueryResult> queryResults = null;
        try {
            String sessionId = (String) redisTemplate.boundValueOps("sessionId").get();
            GetMethod getMethod = new GetMethod(url);
            Optional<String> response = Optional.of(HttpClientUtil.get(getMethod, "JSESSIONID=" + sessionId));
            String result = response.get().substring(response.get().indexOf(">")+1);
            //自己实体类的路径
            queryResults = XmlUtil.stringToClass(result, HostQueryResult.class);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return queryResults;
    }
}
